package com.ruoyi.gp.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.gp.domain.Patient;
import com.ruoyi.gp.domain.vo.ExcelPatientOptionVo;
import com.ruoyi.gp.domain.vo.PatientOptionVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gp.domain.vo.PatientVo;
import com.ruoyi.gp.domain.bo.PatientBo;
import com.ruoyi.gp.service.IPatientService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【患者】
 *
 * @author ruoyi
 * @date 2024-03-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/patient")
public class PatientController extends BaseController {

    private final IPatientService iPatientService;

    /**
     * 查询【患者】列表
     */
    @SaCheckPermission("system:patient:list")
    @GetMapping("/list")
    public TableDataInfo<PatientVo> list(PatientBo bo, PageQuery pageQuery) {
        return iPatientService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出【患者】列表
     */
    @SaCheckPermission("system:patient:export")
    @Log(title = "【患者】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PatientBo bo, HttpServletResponse response) {
        List<PatientVo> list = iPatientService.queryList(bo);
        ExcelUtil.exportExcel(list, "【患者】", PatientVo.class, response);
    }

    /**
     * 获取【患者】详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:patient:query")
    @GetMapping("/{id}")
    public R<PatientVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iPatientService.queryById(id));
    }

    /**
     * 新增【患者】
     */
    @SaCheckPermission("system:patient:add")
    @Log(title = "【患者】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatientBo bo) {
        return toAjax(iPatientService.insertByBo(bo));
    }

    /**
     * 修改【患者】
     */
    @SaCheckPermission("system:patient:edit")
    @Log(title = "【患者】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatientBo bo) {
        return toAjax(iPatientService.updateByBo(bo));
    }

    /**
     * 删除【患者】
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:patient:remove")
    @Log(title = "【患者】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPatientService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
    /**
     * 查询【后台首页展示】列表
     */
    @SaCheckPermission("system:patient:list")
    @GetMapping("/patients")
    public TableDataInfo<Patient> queryByCount(@RequestParam(value = "doctorId",required = false) Long doctorId,PageQuery pageQuery) {
        return iPatientService.queryByCount(doctorId, pageQuery);
    }


    /**
     * 查询【后台首页展示】列表
     */
    @SaCheckPermission("system:patient:list")
    @GetMapping("/lists")
    public TableDataInfo<Patient> queryByDoctorCount(@RequestParam(value = "doctorId",required = false) Long doctorId,PageQuery pageQuery) {
        return iPatientService.queryByCount(doctorId, pageQuery);
    }
    /**
     * 查询【患者访视量详细信息】列表
     */
    @SaCheckPermission("system:patient:list")
    @Log(title = "患者随访量详细信息")
    @GetMapping("/options/{optionsId}")
    public R<List<ExcelPatientOptionVo>> queryByOption(@NotNull(message = "主键不能为空")@PathVariable Long optionsId) {
        return R.ok(iPatientService.queryByOption(optionsId));
    }


    /**
     * 导出【患者随访量表】列表
     */
    @SaCheckPermission("system:patient:export")
    @Log(title = "【导出单个患者随访量表】", businessType = BusinessType.EXPORT)
    @PostMapping("/export/options")
    public void optionsExport(PatientBo bo, HttpServletResponse response) {
        List<ExcelPatientOptionVo> excelPatientOptionVos = iPatientService.queryByOption(bo.getId());
        ExcelUtil.exportExcel(excelPatientOptionVos, bo.getName()+"患者随访量表", ExcelPatientOptionVo.class, response);
    }
    }


